const myServer = require("../server/my")
class myControl {
  async userinfo(req, res) {
    const result = await myServer.getuserinfo(req.auth.username)
    if (result[0].length) {
      return res.send({
        status: 0,
        data: { ...result[0][0], password: "" },
      })
    } else {
      res.cc("服务器忙,请稍后重试")
    }
  }
  async updatauserinfo(req, res) {
    const { id, nickname, email } = req.body
    const result = await myServer.updatauserinfo(id, nickname, email)
    if (result[0].affectedRows) {
      res.cc("更新成功", 0)
    } else {
      res.cc("服务器忙,请稍后重试")
    }
  }
  async updatauserpassword(req, res) {
    const res1 = await myServer.checkpassword(
      req.auth.username,
      req.body.oldPwd
    )
    if (res1[0].length) {
      const result = await myServer.updatauserpassword(
        req.auth.username,
        req.body.newPwd
      )
      if (result[0].affectedRows) {
        res.cc("更新成功", 0)
      } else {
        res.cc("服务器忙,请稍后重试")
      }
    } else {
      res.cc("原密码错误")
    }
  }
  async updataavatar(req, res) {
    const result = await myServer.updatauseravatar(
      req.auth.username,
      req.body.avatar
    )
    if (result[0].affectedRows) {
      res.cc("更新成功", 0)
    } else {
      res.cc("服务器忙,请稍后重试")
    }
  }
}

module.exports = new myControl()
